package com.yndsoft.jc.core;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;


import com.yndsoft.jc.MainActivity;
import com.yndsoft.jc.utils.LogUtils;

import java.util.Iterator;
import java.util.List;
import java.util.Stack;

/**
 * activity堆栈管理
 *
 * @author bichao
 */
public class ActivitiesManager {

    // activity栈
    private static Stack<Activity> mActivityStack;
    private static ActivitiesManager mActivitiesManager;

    private ActivitiesManager() {

    }

    public static ActivitiesManager getInstance() {
        if (null == mActivitiesManager) {
            mActivitiesManager = new ActivitiesManager();
            if (null == mActivityStack) {
                mActivityStack = new Stack<Activity>();
            }
        }
        return mActivitiesManager;
    }

    public int stackSize() {
        return mActivityStack.size();
    }

    /**
     * 获取当前activity
     *
     * @return 当前activity
     */
    public Activity getCurrentActivity() {
        Activity activity = null;

        try {
            activity = mActivityStack.lastElement();
        } catch (Exception e) {
            return null;
        }

        return activity;
    }

    /**
     * 出栈
     * 暂时没有用到 2014-06-05
     */
    public void popActivity() {
        Activity activity = mActivityStack.lastElement();
        if (null != activity) {
            LogUtils.i("popActivity-->" + activity.getClass().getSimpleName());
            activity.finish();
            mActivityStack.remove(activity);
            activity = null;
        }
    }

    /**
     * 出栈
     *
     * @param activity
     */
    public void popActivity(Activity activity) {
        if (null != activity) {
            LogUtils.i("popActivity-->" + activity.getClass().getSimpleName());
            mActivityStack.remove(activity);
            activity = null;
        }
    }

    /**
     * activity入栈
     *
     * @param activity
     */
    public void pushActivity(Activity activity) {
        if (activity instanceof MainActivity) {
            popAllActivities();
        }
        mActivityStack.add(activity);
        LogUtils.i("pushActivity-->" + activity.getClass().getSimpleName());
    }

    /**
     * 所有activity出栈
     */
    public void popAllActivities() {
        while (!mActivityStack.isEmpty()) {
            Activity activity = getCurrentActivity();
            if (null == activity) {
                break;
            }
            activity.finish();
            popActivity(activity);
        }
    }


    /**
     * 指定的activity出栈
     */
    public void popSpecialActivity(Class<?> cls) {
        try {
            Iterator<Activity> iterator = mActivityStack.iterator();
            Activity activity = null;
            while (iterator.hasNext()) {
                activity = iterator.next();
                if (activity.getClass().equals(cls)) {
                    activity.finish();
                    iterator.remove();
                    activity = null;
                }
            }
        } catch (Exception e) {

        }
    }

    /**
     * 遍历目前栈中的activity
     */
    public void peekActivity() {
        for (Activity activity : mActivityStack) {
            if (null == activity) {
                break;
            }
            LogUtils.i("peekActivity()-->"
                    + activity.getClass().getSimpleName());
        }
    }

    public boolean contains(Class<? extends Activity> clazz) {
        try {
            Iterator<Activity> iterator = mActivityStack.iterator();
            Activity activity = null;
            while (iterator.hasNext()) {
                activity = iterator.next();
                if (activity.getClass().equals(clazz)) {
                    return true;
                }
            }
        } catch (Exception e) {

        }

        return false;
    }

    /**
     * 获取指定的界面
     */
    public Activity getSpecialActivity(Class<?> cls) {
        try {
            Iterator<Activity> iterator = mActivityStack.iterator();
            Activity activity = null;
            while (iterator.hasNext()) {
                activity = iterator.next();
                if (activity.getClass().equals(cls)) {
                    return activity;
                }
            }
        } catch (Exception e) {

        }
        return null;
    }

    /**
     *判断当前应用程序处于前台还是后台
     */
    public static boolean isApplicationBroughtToBackground(final Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
                return true;
            }
        }
        return false;
    }

}
